package com.toe.order.webapi.repository.impl;

import com.toe.order.webapi.repository.IOrderGoodsRedisRepository;
import com.toe.pojo.vo.order.OrderGoodsDetailsVO;
import com.toe.pojo.vo.order.OrderGoodsSimpleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Repository
public class OrderGoodsRedisRepositoryImpl implements IOrderGoodsRedisRepository {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean hasDetailsData(Long orderId) {
        String key = KEY_ORDER_GOODS_DETAILS + orderId;
        return redisTemplate.hasKey(key);
    }

    @Override
    public boolean hasUserSimpleData(Long userId) {
        String key = KEY_ORDER_GOODS_SIMPLE + userId;
        return redisTemplate.hasKey(key);
    }

    @Override
    public void saveDetailsEmptyValue(Long orderId) {
        String key = KEY_ORDER_GOODS_DETAILS + orderId;
        redisTemplate.boundValueOps(key).set(null, 15, TimeUnit.MINUTES);
    }

    @Override
    public void saveSimpleEmptyValue(Long userId) {

    }

    @Override
    public void saveDetailsValue(OrderGoodsDetailsVO orderGoods) {
        String key = KEY_ORDER_GOODS_DETAILS + orderGoods.getId();
        redisTemplate.boundValueOps(key).set(orderGoods, 15, TimeUnit.MINUTES);
    }

    @Override
    public void saveSimpleValue(List<OrderGoodsSimpleVO> listOrderSimple) {

    }

    @Override
    public void deleteDetailsValue(Long orderId) {
        String key = KEY_ORDER_GOODS_DETAILS + orderId;
        redisTemplate.delete(key);
    }

    @Override
    public OrderGoodsDetailsVO getOrderDetails(Long orderId) {
        String key = KEY_ORDER_GOODS_DETAILS + orderId;
        return (OrderGoodsDetailsVO) redisTemplate.boundValueOps(key).get();
    }

    @Override
    public List<OrderGoodsSimpleVO> getOrderSimple(List<Long> orderId) {
        return null;
    }

}
